#!/bin/bash

##############################################
#                                            #
#    ServerManager for Trinity               #
#    supports second realm as testserver     #
#    by: Heiner                              #
#                                            #
##############################################

#Version is x.y.z
#x - Release
#y - Available complete features
#z - Changes and improvements
VERSION="1.5.1"

useScreen=$(awk '$1=="useScreen" {print $3}' servermanager.conf)

##############################################

function stop() {

	local server=$1

	case $server in
		authserver)
			kill $APID
		;;
		worldserver)
			screen -S WorldServer -p 0 -X stuff $'server shutdown 1\n'
		;;
		testserver)
			screen -S TestServer -p 0 -X stuff $'server shutdown 1\n'
		;;
	esac
			
}

function getScreen() {

	case $1 in
		authserver)
			if [ -n "$APID" ]; then
				let "SCR = $APID"
				screen -r $SCR
			else
				echo "AuthServer is not running! Cant get screen"
			fi
			
		;;

		worldserver)
			if [ -n "$WPID" ]; then
				let "SCR = $WPID"
				screen -r $SCR
			else
				echo "WorldServer is not running! Cant get screen"
			fi
		;;

		testserver)
			if [ -n "$TPID" ]; then
				let "SCR = $TPID"
				screen -r $SCR
			else
				echo "TestServer is not running! Cant get screen"
			fi
		;;
	esac

}

input=$1

if [ -n "$input" ]; then
	echo $VERSION
	exit
fi

echo ServerManager $VERSION for trinityservers
echo Type help for availabe commands

while : true
do

APID=$(ps -A -o pid,cmd | awk '$2=="SCREEN" && $4=="AuthServer" {print $1}')
WPID=$(ps -A -o pid,cmd | awk '$2=="SCREEN" && $4=="WorldServer" {print $1}')
TPID=$(ps -A -o pid,cmd | awk '$2=="SCREEN" && $4=="TestServer" {print $1}')
OPID=$(ps -A -o pid,cmd | awk '$2=="SCREEN" && $4=="Observer" {print $1}')

read -p "ServerManager>" firstc secondc thirdc fourthc

case $firstc in
	authserver)
		case $secondc in
			check)
				./checkStatus authserver
			;;
			start)
				./start authserver
			;;
			getscr)
				getScreen authserver
			;;
			stop)
				stop authserver
				echo "AuthServer stoped!"
			;;
			*)
				echo What do you want to do with the AuthServer? 
			;;
		esac	

	;;
	worldserver)
		case $secondc in
			check)
				./checkStatus worldserver
			;;
			start)
				./start worldserver 
			;;
			getscr)
				getScreen worldserver
			;;
			stop)
				stop worldserver
				echo "WorldServer stoped!"
			;;
			*)
				echo What do you want to do with the WorldServer?
			;;
		esac
	;;
	testserver)
		case $secondc in
			check)
				./checkStatus testserver
			;;
			start)
				./start testserver 
			;;
			getscr)
				getScreen testserver 
			;;
			stop)
				stop testserver	
				echo "TestServer stoped!"
			;;
			*)
				echo What do you want to do with the TestServer?
			;;
		esac
	;;
	all)
		case $secondc in
			check)
				./checkStatus authserver
				./checkStatus worldserver
				./checkStatus testserver
			;;
			start)
				./start authserver
				./start worldserver
				./start testserver
			;;
			restart)
				echo TODO
				#restart authserver
				#restart worldserver
				#restart testserver
			;;
			*)
				echo What do you want to do with all your Servers?
			;;
		esac
	;;
	observer)
		case $secondc in
			start)
			    if [ -z "$OPID" ]; then
				
				./checkStatus observermode authserver worldserver testserver
				exitCode=$?
				###############################################
				#
				#  exitCodes:
				#
				#  1: only authserver is running
				#  2: only worldserver is running
				#  3: authserver and worldserver are running
				#  4: only testserver is runnung
				#  5: authserver and testserver are running
				#  6: worldserver and testserver are running
				#  7: all three servers are running
				#

				case $exitCode in
					1)
						if [ $useScreen -eq 1 ]; then
							screen -AmdS Observer ./observer 1 authserver
						else
							./observer 1 authserver
						fi
				 	;;
					2)
						if [ $useScreen -eq 1 ]; then
							screen -AmdS Observer ./observer 2 worldserver
						else
							./observer 2 worldserver
						fi
					;;
					3)
						if [ $useScreen -eq 1 ]; then
							screen -AmdS Observer ./observer 3 authserver worldserver
						else
							./observer 3 authserver worldserver
						fi
					;;
					4)
						if [ $useScreen -eq 1 ]; then
							screen -AmdS Observer ./observer 4 testserver
						else
							./observer 4 testserver
						fi
					;;
					5)
						if [ $useScreen -eq 1 ]; then
							screen -AmdS Observer ./observer 5 authserver testserver
						else
							./observer 5 authserver testserver
						fi
					;;
					6)
						if [ $useScreen -eq 1 ]; then
							screen -AmdS Observer ./observer 6 worldserver testserver
						else
							./observer 6 worldserver testserver
						fi
					;;
					7)
						if [ $useScreen -eq 1 ]; then
							screen -AmdS Observer ./observer 7 authserver worldserver testserver
						else
							./observer 7 authserver worldserver testserver
						fi
					;;
				esac
			    else
				echo "Observer is already running!"	
			    fi
			    
			;;
			stop)
				kill $OPID
				echo "Observer stoped!"
			;;
			getscr)
				
				if [ -n "$OPID" ]; then
					screen -r $OPID
				else
					echo "The Observer is not active! Cant get screen"
				fi
	
			;;
			check)
				if ([ -d "/proc/"$OPID ] && [ -n "$OPID" ]); then
					echo "The Observer is running! $(date) PID:$OPID"
				else
					echo "The Observer is not active! $(date)"
				fi
			;;
			*)
				echo Invalid command
			;;
		esac
	;;
	exit)
		exit	
	;;
	help)
		echo "------------------------------------------------------"
		echo "Usage: [server] [command]"
		echo ""
		echo "[server]:"
		echo "authserver, worldserver, testserver, all"
		echo "Type exit to quit ServerManager"
		echo ""
		echo "[command]:"
		echo "check	- check if the server is running"
		echo "start	- start the specified server"
		echo "stop	- stop the specified server"
		echo "getscr	- open the server's screen-window"
		echo "		Type CTRL-A D to detach and get"
		echo "		back to ServerManager"
		echo ""
		echo "------------------------------------------------------"
		echo ""
		echo "Usage: observer [command]"
		echo ""
		echo "[command]:"
		echo "start	- Check for running server and start"
		echo "		to observe them"
		echo "check	- check if the observer is running"
		echo "getscr	- open the server's screen-window"
		echo "stop	- stop observing the servers"
		echo "		not yet implemented - use getscr"
		echo "		instead and type CTRL-C to stop the"
		echo "		observer"
		echo ""
		echo "------------------------------------------------------"
		echo ""
		
	;;
	*)
		echo Invalid Command
	;;
esac

done
